package com.yuyuto.conn.exception;

import android.text.TextUtils;

/**
 * Created by Demon on 2016/11/25.
 */

public class ResultException extends RuntimeException {

    public int code = 0;
    public static final int SYSTEM_ERROR = 20000;
    public static final int PARAM_CHECK_FAILED = 20001;
    public static final int TOKEN_INVALID = 20002;
    public static final int AUTH_CODE_EMPTY = 20003;
    public static final int ENCRYPT_FAIL = 20004;
    public static final int USER_EXIST = 20005;
    public static final int NEED_REGISTER = 20006;
    public static final int USER_PWD_ERROR = 20007;
    public static final int VERIFICATION_EXPIRED = 20008;
    public static final int VERIFICATION_ERROR = 20009;
    public static final int TITLE_NULL = 30001;
    public static final int PUBLIC_TAG_NULL = 30002;
    public static final int TOP_MAX = 40001;
    public static final int MEMBER_CODE_NULL = 40002;
    public static final int COMMENT_EXIST = 40003;
    public String message;

    public ResultException(int code, String message) {
        this.code = code;
        String msg = getApiExceptionMessage(code);
        if (TextUtils.isEmpty(msg)) {
            this.message = message;
        } else {
            this.message = msg;
        }
    }

    /**
     * 由于服务器传递过来的错误信息直接给用户看的话，用户未必能够理解
     * 需要根据错误码对错误信息进行一个转换，在显示给用户
     *
     * @param code
     * @return
     */
    private String getApiExceptionMessage(int code) {
        String message = null;
        switch (code) {
            case SYSTEM_ERROR:
                message = "系统错误";
                break;
            case PARAM_CHECK_FAILED:
                message = "参数校验失败";
                break;
            case TOKEN_INVALID:
                message = "session失效，请重新登录";
                break;
            case AUTH_CODE_EMPTY:
                message = "验证码不能为空";
                break;
            case ENCRYPT_FAIL:
                message = "密码加密失败,请重试";
                break;
            case USER_EXIST:
                message = "用户已存在,请直接登录";
                break;
            case NEED_REGISTER:
                message = "用户名不存在，请先注册";
                break;
            case USER_PWD_ERROR:
                message = "用户名或密码错误";
                break;
            case VERIFICATION_EXPIRED:
                message = "验证码已过期";
                break;
            case VERIFICATION_ERROR:
                message = "验证码不正确";
                break;
            case TITLE_NULL:
                message = "标题不能为空";
                break;
            case PUBLIC_TAG_NULL:
                message = "发布标记不能为空";
                break;
            case TOP_MAX:
                message = "超过最大置顶数量限制";
                break;
            case MEMBER_CODE_NULL:
                message = "会员ID不能为空";
                break;
            case COMMENT_EXIST:
                message = "评论不存在";
                break;
            default:
                message = null;
        }
        return message;
    }

}
